pg-product-simulator.js ➔ findQuantitySelector   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
1
/* global simulatorData  */
2
if (simulatorData.pagantisSimulator4x === 'enabled') {
3
    var simulator4xdiv = document.getElementsByClassName('mainPagantisSimulator');
4
    if (simulator4xdiv.length > 0 && typeof simulator4xdiv != 'undefined') {
5
        simulator4xdiv[0].innerHTML = simulatorData.simulatorMessage;
6
        var position = simulatorData.positionSelector4x;
7
        if (position !== 'default') { // Move to desired class
8
            var desiredPosition = document.getElementsByClassName(position);
9
            if (desiredPosition.length > 0 && typeof desiredPosition != 'undefined') {
10
                desiredPosition[0].appendChild(simulator4xdiv[0]);
11
            }
12
        }
13
    }
14
}
15
16
17
function findPriceSelector() {
18
    var priceSelectors = simulatorData.priceSelector;
19
    if (typeof priceSelectors == 'undefined') console.log("simulator priceSelectors is undefined");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20
    return priceSelectors.find(function (candidateSelector) {
21
        var priceDOM = document.querySelector(candidateSelector);
22
        return (priceDOM != null);
0 ignored issues
show
Best Practice introduced by
Comparing priceDOM to null using the != operator is not safe. Consider using !== instead.
Loading history...
23
    });
24
25
}
26
27
function findPositionSelector() {
28
    var positionSelector = simulatorData.positionSelector;
29
    if (positionSelector === 'default') {
30
        positionSelector = '.pagantisSimulator';
31
    }
32
33
    return positionSelector;
34
}
35
36
function findQuantitySelector() {
37
    var quantitySelectors = simulatorData.quantitySelector;
38
    return quantitySelectors.find(function (candidateSelector) {
39
        var priceDOM = document.querySelector(candidateSelector);
40
        return (priceDOM != null);
0 ignored issues
show
Best Practice introduced by
Comparing priceDOM to null using the != operator is not safe. Consider using !== instead.
Loading history...
41
    });
42
}
43
44
function finishInterval() {
45
    clearInterval(window.loadingSimulator);
46
    return true;
47
}
48
49
function checkSimulatorContent() {
50
    var simulatorLoaded = false;
51
    var positionSelector = findPositionSelector();
52
    var pgDiv = document.querySelectorAll(positionSelector);
53
    if (pgDiv.length > 0 && typeof window.WCSimulatorId != 'undefined') {
54
        var pgElement = pgDiv[0];
55
        if (pgElement.innerHTML !== '') {
56
            simulatorLoaded = true;
57
        }
58
    }
59
    return simulatorLoaded;
60
}
61
62
function findDestinationSim() {
63
    var destinationSim = simulatorData.finalDestination;
64
    if (destinationSim === 'default' || destinationSim == '') {
65
        destinationSim = 'woocommerce-product-details__short-description';
66
    }
67
68
    return destinationSim;
69
}
70
71
function checkAttempts() {
72
    window.attempts = window.attempts + 1;
73
    return (window.attempts > 10)
74
}
75
76
function loadSimulatorPagantis() {
77
    if (typeof pgSDK == 'undefined' || typeof simulatorData == 'undefined') {
0 ignored issues
show
Bug introduced by
The variable pgSDK seems to be never declared. If this is a global, consider adding a /** global: pgSDK */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
78
        return false;
79
    }
80
81
    if (checkAttempts() || checkSimulatorContent()) {
82
        return finishInterval();
83
    }
84
85
    var country = simulatorData.country;
86
    var locale = simulatorData.locale;
87
    var sdk = pgSDK;
88
89
    var positionSelector = findPositionSelector();
90
    var priceSelector = findPriceSelector();
91
    var promotedProduct = simulatorData.promoted;
92
    var quantitySelector = findQuantitySelector();
93
94
    var simulator_options = {
95
        publicKey: simulatorData.public_key,
96
        type: eval(simulatorData.simulator_type),
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
97
        selector: positionSelector,
98
        itemQuantitySelector: quantitySelector,
99
        locale:locale,
100
        country: country,
101
        itemAmountSelector: priceSelector,
102
        amountParserConfig: {
103
            thousandSeparator: simulatorData.thousandSeparator,
104
            decimalSeparator: simulatorData.decimalSeparator,
105
        },
106
        numInstalments: simulatorData.pagantisQuotesStart,
107
        skin: eval(simulatorData.pagantisSimulatorSkin),
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
108
        position: eval(simulatorData.pagantisSimulatorPosition),
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
109
    };
110
111
    window.pgSDK = sdk;
112
    if (promotedProduct === 'true') {
113
        simulator_options.itemPromotedAmountSelector = priceSelector;
114
    }
115
116
    if (typeof window.pgSDK !== 'undefined') {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if typeof window.pgSDK !== "undefined" is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
117
        window.WCSimulatorId = window.pgSDK.simulator.init(simulator_options);
118
        return false;
119
    }
120
}
121
122
window.attempts = 0;
123
if (simulatorData.pagantisSimulator === 'enabled') {
124
    window.loadingSimulator = setInterval(function () {
125
        loadSimulatorPagantis();
126
    }, 2000);
127
}
128
129
if (simulatorData.promoted === 'true') {
130
    simulatorData.promotedMessage;
0 ignored issues
show
introduced by
The result of the property access to simulatorData.promotedMessage is not used.
Loading history...
131
}
132
133